package com.sx.pipeline.ui.login.frag

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.LoginType
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.databinding.FragAccountLoginBinding
import com.sx.pipeline.ui.login.LoginPageActivity
import com.sx.pipeline.utils.ImageCodeUtils

/**
 * @Author: YY
 * @Date: 2022/4/6
 * @Description: 账号登录
 */
class AccountLoginFragment: CYDataBindingBaseFragment<FragAccountLoginBinding, LoginVM>(
    R.layout.frag_account_login), View.OnClickListener {

    companion object {
        fun newInstance(position: Int) =
            AccountLoginFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, position)
                }
            }
    }

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        // 随机码
        mBinding.imageCode.setImageBitmap(ImageCodeUtils.getInstance().createBitmap())
    }

    override fun initViewObservable() {
        super.initViewObservable()
    }

    override fun onClick(v: View) {
        when(v.id) {
            R.id.image_code -> {
                // 随机验证码
                mBinding.imageCode.setImageBitmap(ImageCodeUtils.getInstance().createBitmap())
            }
            R.id.tv_register_user -> {
                // 注册
                RouteManager.Register.intentToRegSelectTypeID()
            }
            R.id.tv_forget_pwd -> {
                // 忘记密码
                RouteManager.ForgetPwd.intentToForgetPwd()
            }
            R.id.tv_login -> {
                // 登录
                if((activity as LoginPageActivity).isCheck()){
                    if (ImageCodeUtils.getInstance().code.equals(mBinding.etGraphCode.text.toString().trim())) {
                        // 登录
                        val phone = mBinding.etPhone.text.toString()
                        val pwd = mBinding.etPwd.text.toString()
                        mViewModel.login(LoginType.ACCOUNT_LOGIN.code, phone, "",  pwd)
                    } else {
                        ToastUtils.showShort("图形验证码不正确")
                    }
                }

            }
        }
    }
}